//
//  AccountLogoutVIew.m
//  location
//
//  Created by Dongxiang Xu on 2025/5/29.
//

#import "AccountLogoutView.h"
#import "PolicyWebViewController.h"

@implementation AccountLogoutView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;
        [self initView];
    }
    return self;
}

- (void)initView
{
    UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(12, SCREEN_HEIGHT, SCREEN_WIDTH-24, 388)];
    infoView.layer.masksToBounds = YES;
    [infoView.layer setCornerRadius:40];
    infoView.backgroundColor = RealWhiteColor;
    [self addSubview:infoView];
    
    UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(infoView.width-46, 16, 30, 30)];
    [close setImage:[UIImage imageNamed:@"map_close"] forState:UIControlStateNormal];
    [infoView addSubview:close];
    [[close rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.3 animations:^{
            infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake((infoView.width-42)/2, 44, 42, 42)];
    img.image = [UIImage imageNamed:@"log_out"];
    [infoView addSubview:img];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, img.maxY+32, infoView.width, 28)];
    title.textAlignment = NSTextAlignmentCenter;
    title.text = @"建议完成以下事项后在注销";
    title.textColor = BlackColor;
    title.font = MEDIUM_FONT(20);
    [infoView addSubview:title];
    
    
    
    UIView *detailView = [[UIView alloc] initWithFrame:CGRectMake(32, title.maxY+8, infoView.width-64, 0)];
    [infoView addSubview:detailView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, detailView.width, 24)];
    label.text = @"担任家庭管理员";
    label.textColor = GraryHeavyLightColor;
    label.font = MEDIUM_FONT(14);
    [detailView addSubview:label];
    
    UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, label.maxY, detailView.width-22, 44)];
    subLabel.text = @"确认注销后，将自动移交管理员 会员未到期 确认注销后，未到期的订阅无法恢复";
    subLabel.textColor = GraryLightColor;
    subLabel.font = DEFAULT_FONT(13);
    subLabel.numberOfLines = 2;
    [detailView addSubview:subLabel];
    
    UIImageView *warn = [[UIImageView alloc] initWithFrame:CGRectMake(detailView.width-22, subLabel.y, 22, 22)];
    warn.image = [UIImage imageNamed:@"warnning_logout"];
    [detailView addSubview:warn];
    
    UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, subLabel.maxY+8, detailView.width, 24)];
    aLabel.text = @"会员未到期";
    aLabel.textColor = GraryHeavyLightColor;
    aLabel.font = MEDIUM_FONT(14);
    [detailView addSubview:aLabel];
    
    UILabel *aSubLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, aLabel.maxY, detailView.width-22, 22)];
    aSubLabel.text = @"确认注销后，未到期的订阅无法恢复";
    aSubLabel.textColor = GraryLightColor;
    aSubLabel.font = DEFAULT_FONT(13);
    [detailView addSubview:aSubLabel];
    
    UIImageView *aWarn = [[UIImageView alloc] initWithFrame:CGRectMake(detailView.width-22, aLabel.y+12, 22, 22)];
    aWarn.image = [UIImage imageNamed:@"warnning_logout"];
    [detailView addSubview:aWarn];
    
    detailView.height = aSubLabel.maxY;
    
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"注销前请认真阅读以下重要提醒。账号注销后，您将无法再使用该账号，包括但不限于:\n\n1.无法登录、使用家人足迹账号，并移除该账号下所有登录方式\n2.该账号下的个人资料和数据信息都将无法找回\n3.仅当前账号存在的家庭，生效中的会员订阅无法恢复\n4.与当前账号有关的资产及权益将被清除"];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];
    UILabel *subTitle = [[UILabel alloc] initWithFrame:CGRectMake(32, 96, infoView.width-64, 204)];
    subTitle.attributedText = attributedString;
    subTitle.textColor = GraryHeavyLightColor;
    subTitle.font = DEFAULT_FONT(14);
    subTitle.numberOfLines = 0;
    subTitle.alpha = 0;
    subTitle.y = 96;
    [infoView addSubview:subTitle];
    
    UIView *agreeView = [[UIView alloc] initWithFrame:CGRectMake(32, subTitle.maxY+22, infoView.width-64, 106)];
    agreeView.alpha = 0;
    [infoView addSubview:agreeView];
    
    UIButton *agree = [[UIButton alloc] initWithFrame:CGRectMake((agreeView.width-236)/2, 0, 30, 30)];
    [agree setImage:[UIImage imageNamed:@"agree_nor"] forState:UIControlStateNormal];
    [agree setImage:[UIImage imageNamed:@"agree_sel"] forState:UIControlStateSelected];
    [agreeView addSubview:agree];
    
    UILabel *agreeLabel = [[UILabel alloc] initWithFrame:CGRectMake(agree.maxX, 0, 206, 30)];
    agreeLabel.userInteractionEnabled = YES;
    NSMutableAttributedString *agreeString = [[NSMutableAttributedString alloc]initWithString:@"我已阅读并知晓了《账号注销须知》"];
    [agreeString addAttribute:NSForegroundColorAttributeName value:NormalMainColor range:[agreeString.string rangeOfString:@"《账号注销须知》"]];
    agreeLabel.font = DEFAULT_FONT(13);
    agreeLabel.textColor = GraryTooHeavyColor;
    agreeLabel.attributedText = agreeString;
    [agreeView addSubview:agreeLabel];
    
    UIButton *logout = [[UIButton alloc] initWithFrame:CGRectMake(agreeLabel.width-100, 0, 100, agreeLabel.height)];
    [agreeLabel addSubview:logout];
    [[logout rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        PolicyWebViewController *vc = [[PolicyWebViewController alloc] initWithRelativePath:@"http://m.tanhuos.com/logout_agreement.html"];
        vc.modalPresentationStyle = UIModalPresentationPageSheet;
        [self.viewContainingController presentViewController:vc animated:YES completion:nil];
    }];
    
    UIButton *agreeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, agreeLabel.maxY+22, agreeView.width, 54)];
    agreeButton.layer.masksToBounds = YES;
    [agreeButton.layer setCornerRadius:27];
    [agreeButton setTitle:@"我确认上述内容" forState:UIControlStateNormal];
    [agreeButton.titleLabel setFont:MEDIUM_FONT(17)];
    agreeButton.backgroundColor = LightBGColor;
    [agreeButton setTitleColor:ThemeDark2 forState:UIControlStateNormal];
    [agreeView addSubview:agreeButton];
    
    [[agree rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (agree.selected) {
            agree.selected = NO;
            [agreeButton setTitleColor:ThemeDark2 forState:UIControlStateNormal];
        }else{
            agree.selected = YES;
            [agreeButton setTitleColor:GraryHeavyLightColor forState:UIControlStateNormal];
        }
    }];
    
    [[agreeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (agree.selected) {
            [UIView animateWithDuration:0.3 animations:^{
                [infoView setFrame:CGRectMake(12, SCREEN_HEIGHT-344, SCREEN_WIDTH-24, 312)];
                img.alpha = 1;
                title.y = img.maxY+32;
                title.text = @"确认注销？";
                agreeView.alpha = 0;
                                
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"注销账号后，账号内的所有内容将一并删除， 并无法恢复"];
                NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
                [paragraphStyle setLineSpacing:5];
                [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];
                subTitle.attributedText = attributedString;
                subTitle.numberOfLines = 0;
                [subTitle setFrame:CGRectMake(32, title.maxY+6, infoView.width-64, 48)];
                subTitle.textColor = GraryLightColor;
                
                [self viewWithTag:50].y = infoView.height-84;
                [self viewWithTag:51].y = infoView.height-84;
                [self viewWithTag:50].alpha = 1;
                [self viewWithTag:51].alpha = 1;
            }];
        }
    }];
    
    NSArray *array = @[@"不是现在",@"确认注销"];
    for (int i = 0; i < array.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(32+i*((infoView.width-86)/2+22), infoView.height-84, (infoView.width-86)/2, 54)];
        button.tag = 50+i;
        button.layer.masksToBounds = YES;
        [button.layer setCornerRadius:24];
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button.titleLabel setFont:MEDIUM_FONT(17)];
        if (i==0) {
            button.backgroundColor = LightBGColor;
            [button setTitleColor:RealBlackColor forState:UIControlStateNormal];
        }else{
            button.backgroundColor = RedColor;
            [button setTitleColor:RealWhiteColor forState:UIControlStateNormal];
        }
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (i==0) {
                [UIView animateWithDuration:0.3 animations:^{
                    infoView.y = SCREEN_HEIGHT;
                    self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
                } completion:^(BOOL finished) {
                    [self removeFromSuperview];
                }];
            }else{
                if (infoView.height==388) {
                    [UIView animateWithDuration:0.3 animations:^{
                        [infoView setFrame:CGRectMake(12, SCREEN_HEIGHT-490, SCREEN_WIDTH-24, 458)];
                        img.alpha = 0;
                        title.y = 40;
                        detailView.alpha = 0;
                        subTitle.alpha = 1;
                        [self viewWithTag:50].alpha = 0;
                        [self viewWithTag:51].alpha = 0;
                        agreeView.alpha = 1;
                    }];
                }else if (infoView.height==312){
                    [UIView animateWithDuration:0.3 animations:^{
                        infoView.y = SCREEN_HEIGHT;
                        self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
                    } completion:^(BOOL finished) {
                        [self removeFromSuperview];
                    }];
                    
                    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
                    manager.requestSerializer = [AFJSONRequestSerializer serializer];
                    manager.responseSerializer = [AFJSONResponseSerializer serializer];
                    manager.requestSerializer.timeoutInterval = 10;
                    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"image/jpeg",@"image/png",@"application/octet-stream",@"text/json",nil];
                    NSMutableDictionary *param = [NSMutableDictionary dictionary];
                    [manager DELETE:[NSString stringWithFormat:@"%@/system/user/remove",NETURL] parameters:param headers:@{@"Authorization":[XDXShareInstance shareInstance].access_token} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                        if ([responseObject[@"code"] integerValue]==200) {
                            if (self.AccountLogoutBlock) {
                                self.AccountLogoutBlock();
                            }
                        }
                    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                        
                    }];
                }
            }
        }];
        [infoView addSubview:button];
    }
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self insertSubview:bgView atIndex:0];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        [UIView animateWithDuration:0.3 animations:^{
            infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    [bgView addGestureRecognizer:tap];
    
    [UIView animateWithDuration:0.3 animations:^{
        infoView.y = SCREEN_HEIGHT-infoView.height-32;
        self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0.4];
    }];
}

@end
